package spring.cloud.tasks.executor.executor.task.task_configuration;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.common.nodepaths.TaskBaseInfoNode;
import spring.cloud.tasks.common.nodepaths.TaskNodePaths;
import spring.cloud.tasks.executor.executor.task.TaskExecutor;
import spring.cloud.tasks.executor.executor.task.TaskListenerHolder;

@Slf4j
@Setter
@Getter
public class TaskConfigurationListenerHolder extends TaskListenerHolder {
    public boolean isShutdown = false;

    public TaskConfigurationListenerHolder(TaskExecutor taskExecutor) {
        super(taskExecutor);
        taskId = taskExecutor.getTaskConfiguration().getTaskId();
        taskConfiguration = taskExecutor.getTaskConfiguration();
    }

    @Override
    public void start() {
        taskExecutorNodeCacheOrTreeCacheManager.addTreeCacheListener(TaskNodePaths.getTaskNodePath(taskId, TaskBaseInfoNode.baseInfoNode), 0, new ReloadTaskConfigurationUpdateListener(this));
        taskExecutorNodeCacheOrTreeCacheManager.addTreeCacheListener(TaskNodePaths.getTaskNodePath(taskId, TaskNodePaths.to_delete), 0, new DeleteTaskListener(this));
        taskExecutorNodeCacheOrTreeCacheManager.addTreeCacheListener(TaskNodePaths.getTaskNodePath(taskId, TaskNodePaths.enabled), 0, new DisableTaskListener(this));

    }

    @Override
    public void stop() {
        super.stop();
        isShutdown = true;
        taskExecutorNodeCacheOrTreeCacheManager.closeTreeCache(TaskNodePaths.getTaskNodePath(taskId, TaskBaseInfoNode.baseInfoNode), 0);
        taskExecutorNodeCacheOrTreeCacheManager.closeTreeCache(TaskNodePaths.getTaskNodePath(taskId, TaskNodePaths.to_delete), 0);
        taskExecutorNodeCacheOrTreeCacheManager.closeTreeCache(TaskNodePaths.getTaskNodePath(taskId, TaskNodePaths.enabled), 0);
    }

}
